/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-04-24 17:26:14
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-10-07 15:12:28
 * @FilePath: \cxl-admin\src\pages\operation\product\singleDialog\index.ts
 */
import { addProduct, editProduct, productInterface } from '@/apis/operation/product'
import { ElMessage, FormInstance } from 'element-plus'
import { reactive } from 'vue'

export const ProductTypeList: string[] = ['出险车况查询', '维保车况查询', '新能源车况查询']

export enum ProductType {
    '出险车况查询' = 1,
    '维保车况查询',
    '新能源车况查询'
}

export class SinglePrice {
    TABLE_TYPE = reactive([...ProductTypeList])
    config = reactive({
        productType: '出险车况查询', //产品类型
        defaultPrice: '', //默认价格
        promotionPrice: '', //活动价格
        expire: '', //时效性
        id: '',
        type: 'create' //create | edit
    })
    //时效性
    expireOptions = reactive([
        { label: '长期有效', value: '-1' },
        { label: '15天', value: '2' },
        { label: '30天', value: '3' },
        { label: '60天', value: '4' }
    ])
    configRules = reactive({
        type: [{ required: true, message: '请选择产品类型', trigger: 'change' }],
        defaultPrice: [{ required: true, message: '请输入价格', trigger: 'blur' }],
        promotionPrice: [{ required: true, message: '请输入价格', trigger: 'blur' }],
        expire: [{ required: true, message: '请选择时效性', trigger: 'change' }]
    })
    onSelectTableType = (res: any) => {
        console.log(res)
    }
    /**
     * 重置数据
     */
    resetData = () => {
        this.config.productType = this.TABLE_TYPE[0]
        this.config.defaultPrice = ''
        this.config.promotionPrice = ''
        this.config.expire = ''
    }

    /**
     * 确定编辑
     * @param formEl
     */
    onComfirmEdit = async (formEl: FormInstance | undefined, callback: () => void) => {
        if (!formEl) return
        await formEl.validate((valid, fields) => {
            if (valid) {
                console.log(this.config.productType, this.TABLE_TYPE[0])
                //productType产品类型（1.车况-出险；2.车况-维保车况 3 新能源车况）

                let productType = 2

                if (this.config.productType == ProductType[1]) {
                    productType = 1
                } else if (this.config.productType == ProductType[2]) {
                    productType = 2
                } else if (this.config.productType == ProductType[3]) {
                    productType = 3
                }

                const obj: productInterface = {
                    basicPrice: Number(this.config.defaultPrice),
                    productType: productType,
                    singlePrice: Number(
                        (Number(this.config.defaultPrice) - Number(this.config.promotionPrice)).toFixed(2)
                    )
                }
                console.log('obj', obj)
                if (this.config.type === 'create') {
                    //新增
                    addProduct(obj).then(res => {
                        if (res.data.value === 200) {
                            ElMessage.success('新增成功！')
                            callback()
                        }
                    })
                } else {
                    //更新
                    obj.id = this.config.id
                    editProduct(obj).then(res => {
                        if (res.data.value === 200) {
                            ElMessage.success('修改成功！')
                            callback()
                        }
                    })
                }
            }
        })
    }
}
